import {defineStore} from "pinia";
import {ElMessage} from "element-plus";
import {
    addInformation,
    delInformation,
    findAllInformation,
    getInformation,
    updateInformation,
    updateInformationRecord
} from "@/api/information";

export const InformationStore = defineStore('information', {
    state:()=>{
        return {
            // 列表
            informationList: [],
            // 详细信息
            informationInfo: {}
        }
    },
    actions:{
        updateInformationRecord(id,type){
            return new Promise((resolve, reject)=>{
                updateInformationRecord(id,{type}).then(res => {
                    resolve(res.code)
                    this.getInformationInfo(id)
                }).catch(err => {
                    reject()
                })
            })
        },
        addInformation(){
            return new Promise((resolve, reject)=>{
                addInformation(this.informationInfo).then(res => {
                    resolve(res.result)
                    this.informationInfo = {}
                }).catch(err => {
                    reject()
                })
            })
        },
        async getInformationList(query) {
            return new Promise((resolve, reject)=>{
                findAllInformation(query).then(res => {
                    this.informationList = res.result.list
                    resolve(res.result)
                }).catch(err => {
                    reject()
                })
            })
        },
        async getInformationInfo(id) {
            const {result} = await getInformation(id)
            this.informationInfo = result
            this.informationInfo.information_price = parseFloat(this.informationInfo.information_price)
        },
        updateInformation(id,data) {
            return new Promise(async (resolve, reject) => {
                updateInformation(id,data).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        async deleteInformation(id) {
            const {code} = await delInformation(id)
        },
    }
})